/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2017 All Rights Reserved.
 */
package com.fulihui.information.biz.subscribe;

import com.fulihui.information.dal.dataobj.InfoUserBehaviorReward;
import com.fulihui.information.dal.dataobj.InfoUserBehaviorRewardExample;
import com.fulihui.information.dal.mapper.InfoUserBehaviorRewardMapper;
import com.fulihui.information.enums.AwardType;
import com.fulihui.usercore.sdk.domain.SignTypeEnum;
import com.fulihui.usercore.sdk.domain.UserLoginRegisterMessage;
import com.fulihui.usercore.sdk.pubsub.UserLoginRegisterSubscribeProcessor;
import org.near.toolkit.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 用户注册订阅
 * @author: Levon
 *
 * @version: v 0.1 2017-06-12 10:07
 */
public class UserRegisterSubscribeProcessorImpl implements UserLoginRegisterSubscribeProcessor {

    private static final Logger logger = LoggerFactory.getLogger(UserRegisterSubscribeProcessorImpl.class);

    @Autowired
    InfoUserBehaviorRewardMapper infoUserBehaviorRewardMapper;
    /**
     * 订阅接受到用户注册消息的处理
     *
     * @param message {@link UserLoginRegisterMessage}
     */
    @Override
    public void process(UserLoginRegisterMessage message) {
        if (null == message){
            //忽略
            return;
        }
        if(StringUtil.isBlank(message.getUserId())){
            return;
        }
        //资讯用户注册
        if (StringUtil.equals("INFORMATION_APP",message.getSignSourceCode())
                && StringUtil.equals(SignTypeEnum.SIGN_UP.getCode(),message.getSignType())
                && StringUtil.isNotBlank(message.getMechineNo())){
            InfoUserBehaviorRewardExample example = new InfoUserBehaviorRewardExample();
            InfoUserBehaviorRewardExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(message.getUserId());
            criteria.andRTypeEqualTo(AwardType.REGISTER_CASH.getCode());
            List<InfoUserBehaviorReward> infoUserBehaviorRewards = infoUserBehaviorRewardMapper.selectByExample(example);
            if(CollectionUtils.isEmpty(infoUserBehaviorRewards)){
                logger.info("INFORMATION_APP-新用户注册订阅消息,userId={}",message.getUserId());
                InfoUserBehaviorReward record = new InfoUserBehaviorReward();
                record.setUserId(message.getUserId());
                record.setAmount(0L);
                record.setIsRewarded(0);
                record.setrType(AwardType.REGISTER_CASH.getCode());
                record.setGmtCreate(new Date());
                record.setGmtModifed(new Date());
                infoUserBehaviorRewardMapper.insertSelective(record);
            }
        }

    }
}
